package com.sxx.mall.search.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sxx.commons.dto.es.SkuESModel;
import com.sxx.commons.exception.BizCodeEnum;
import com.sxx.commons.utils.R;
import com.sxx.mall.search.service.ElasticSearchSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @Auther: sunxx
 * @Date: 2024/7/29 - 07 - 29 - 16:52
 * @Description: com.sxx.mall.search.controller
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/es/save")
public class ElasticSearchSaveController {
    @Autowired
    private ElasticSearchSaveService elasticSearchSaveService;
    /*
    * 存储商品上架信息到Elasticsearch服务的接口
    * */
    @PostMapping("product")
    public R productStatusUp(@RequestBody List<SkuESModel> skuESModels) {
        Boolean b =false;
        try {
            b = elasticSearchSaveService.productStatusUp(skuESModels);
        } catch (IOException e) {
            //throw new RuntimeException(e);
            log.error("ElaticeSearch商品上架错误：{}",e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (b){
            return R.ok();
        }
        return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
    }


}
